var GlobalName = null;			//全局命名
var isoLanguageTitleNameJson = {		//JSON名字
	English:{
		winMainTitle:"VIPX_AfterEffectScriptPackage",
		winMarkPanel: "Mark Functions",
		winAddMarkerButtonTitle:"VIPX_AddMarker",
		confirmButtonTitle:"Comfirm",
		cancelButtonTitle:"Cancel",
		ErrorNoItems:"Your project don't have any items!",
		ErrorNoSelectItems:"Please select Composition!",
		ErrorIsNullObj: "This is a null object!",
		FolderItemsName: "VIPX_ToolsFolder",
		CompItemsName:"VIPX_MarkComp",
		CompositionName: "Composition",
		AddMarkTimeLineShowText: "00:00:00:00 00:00:10:00 How to do?\n00:00:10:00 00:00:20:00 How to do?"
	},

	Chinese:{
		winMainTitle:"VIPX_脚本工具包",
		winMarkPanel:"标记功能",
		winAddMarkerButtonTitle:"添加标记",
		confirmButtonTitle:"确定",
		cancelButtonTitle:"取消",
		ErrorNoItems:"你的工程没有任何item!",
		ErrorNoSelectItems:"请选择合成!",
		ErrorIsNullObj: "这是一个空对象",
		FolderItemsName: "VIPX_ToolsFolder",
		CompItemsName:"VIPX_MarkComp",
		CompositionName: "合成",
		AddMarkTimeLineShowText: "00:00:00:00 00:00:10:00 How to do?\n00:00:10:00 00:00:20:00 How to do?"
	}		
};

switch(app.isoLanguage){	//判定系统语言
	case "zh_CN":
		GlobalName = isoLanguageTitleNameJson.Chinese;
	case "en_US":
		GlobalName = isoLanguageTitleNameJson.English;
	default:
		GlobalName = isoLanguageTitleNameJson.English;
}

//函数工具包
function ToolsFunction(){
	var tools = new Object();
	//返回app中选择的Items
	tools.GetSelectItems = function(){
		var video = new Array();
		if(app.project.numItems == 0){
			alert(GlobalName.ErrorNoItems);
			return null;
		}else{
			for(var i=0; i<app.project.numItems; i++){
				if(app.project.items[i+1].selected == true&&app.project.items[i+1].typeName==GlobalName.CompositionName){
					video.push(app.project.items[i+1]);
				}
			}
			if(video.length == 0){
				alert(GlobalName.ErrorNoSelectItems);
				return null;
			}else{return video;}
		}
	}
	//返回app中的指定Folder文件夹
	tools.GetFolderItem = function(name){
		var toolsFolder = null;
		if(app.project.numItems == 0){
			toolsFolder = app.project.items.addFolder(name);
			return toolsFolder;
		}else{
			for(var i=0; i<app.project.numItems; i++){
				if(app.project.items[i+1].name == name){
					toolsFolder = app.project.items[i+1];
				}
			}
			if(toolsFolder == null){
				toolsFolder = app.project.items.addFolder(name);
				return toolsFolder;
			}else{
				return toolsFolder;
			}
		}
	}
	//返回app中的指定合成
	tools.GetCompItem = function(name){
		var comp = null;
		if(app.project.numItems == 0){
			comp = app.project.items.addComp(name,100,100,1.0,3600,30.0);
			return comp;
		}else{
			for(var i=0; i<app.project.numItems; i++){
				if(app.project.items[i+1].name == name){
					comp = app.project.items[i+1];
				}	
			}
			if(comp == null){
				comp = app.project.items.addComp(name,100,100,1.0,3600,30.0);
				return comp;
			}else{
				return comp;
			}
		}
	}
	return tools;
}

function CreateMainUI(){	//创建主窗口
	var winMain = new Window("palette", GlobalName.winMainTitle, undefined,{resizeable:true});

	//添加Mark按钮
	var markerSettingsPanel = winMain.add("Panel", undefined, GlobalName.winMarkPanel);
	markerSettingsPanel.orientation = "row";
	var buttonAddMarker = markerSettingsPanel.add("button", undefined, GlobalName.winAddMarkerButtonTitle);
	buttonAddMarker.onClick = function(){ winAddMark.show(); }
	return winMain;
}

function CreateAddMarkUI(){	//创建标记窗口
	var win = new Window("palette", GlobalName.winAddMarkerButtonTitle, undefined, {resizeable:true});
	win.alignChildren = "center";
	//时间线
	var timeLinePanel = win.add("panel", undefined, "TimeLine:");
	var editInText = timeLinePanel.add("edittext", [0,0,1000,400], GlobalName.AddMarkTimeLineShowText,  {resizeable:true,multiline: true, wantReturn: true});
	//按钮触发
	var groupButton = win.add("group", undefined);
	groupButton.orientation = "row";
	var addMarkerButton = groupButton.add("button", undefined, GlobalName.confirmButtonTitle);
	var cancelButton = groupButton.add("button", undefined, GlobalName.cancelButtonTitle);
	cancelButton.onClick = function(){win.hide();}

	addMarkerButton.onClick = function(){
		var tools = ToolsFunction();
		var compItems = tools.GetSelectItems();
		var toolsFolder = tools.GetFolderItem(GlobalName.FolderItemsName);
		var markComp = tools.GetCompItem(GlobalName.CompItemsName);
		var timeLine = editInText.text;

		if(tools!=null&compItems!=null&toolsFolder!=null&markComp!=null){		//判定是否数据传输正确
			markComp.parentFolder = toolsFolder;
			alert(timeLine);
		}else{
			alert(GlobalName.ErrorIsNullObj);
		}
	}
	return win;
}

var winMain = CreateMainUI();
var winAddMark = CreateAddMarkUI();
winMain.show();

